package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@RequestMapping("/travel")
@Controller
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDestinationService destinationService;


    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") TravelQuery qo){
        //根据请求查询分页数据
        Page<Travel> page = travelService.query(qo);
        //加到模型中
        model.addAttribute("page", page);
        return "travel/list";
    }

    //审核
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(String id,Integer state){
        //调用游记服务类的审核方法进行审核操作
        travelService.changeState(id,state);
        //转到显示
        return JsonResult.success();
    }
    //查看
    @RequestMapping("/getContentById")
    @ResponseBody
    public Object getContentById(String id){
        //调用游记服务类的审核方法进行审核操作
        String content = travelService.getContentById(id);
        //转到显示
        return JsonResult.success(content);
    }


    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel){
        //调用服务层方法
        if (StringUtils.hasLength(travel.getId())){
            //调用更新方法
            travelService.update(travel);
        }else {
            //调用保存方法
            travelService.save(travel);
        }
        //返回执行成功
        return JsonResult.success();
    }


    @RequestMapping("delete")
    @ResponseBody
    public Object delete(String id){
        //删除
        travelService.deleteById(id);
        //返回jsonresult
        return JsonResult.success();
    }
}
